//
//  TestAreaField.swift
//  travel
//
//  Created by tong on 16/6/26.
//  Copyright © 2016年 zhuxietong. All rights reserved.
//

import UIKit
import Eelay

open class TextAreaField: FieldView, UITextViewDelegate
{
    public let valueT = NextGrowingInternalTextView()
    
    
    open override func addLayoutRules() {
        
        self.eelay = [
            [valueT,[ee.T.L.B.R,[0,3,0,-3]]],
        ]
        
        valueT.returnKeyType = .done
        valueT.delegate = self
        valueT.textColor = UIColor(shex: "#444444")
        valueT.font = UIFont.systemFont(ofSize: 13)
        NotificationCenter.default.addObserver(valueT, selector: #selector(resignFirstResponder), name: NSNotification.Name(rawValue: FK.SaveJoFormNotify), object: nil)

        
    }
    
    open override var backgroundColor: UIColor?{
        set{
            super.backgroundColor = newValue
            valueT.backgroundColor = newValue
        }
        get{
            return super.backgroundColor
        }
    }
    
    open override func get_string_value() -> String {
        
        self.valueT.resignFirstResponder()
        mo.model.setObject(self.valueT.text!, forKey: "value" as NSCopying)
        
        if mo.model["value",""].escapeHeadTailSpace() != ""
        {
            return mo.model["value",""].escapeHeadTailSpace()
        }
        
        
        return mo.model[FK.emptyV,""]
    }
    
    
    open func setTitle() {
        
    }
    
    
    open override func  mo_model_reload() {
        
        valueT.text = mo.model[FK.value,""]
        valueT.textColor = FieldView.valueStyle.color
        valueT.font = FieldView.valueStyle.font
//
//            NSAttributedString(string: mo.model[FK.placeholder,""], attributes: [NSAttributedString.Key.font: FieldView.holderStyle.font, NSAttributedString.Key.foregroundColor: FieldView.holderStyle.color])
//        
        self.setTitle()
        
        if mo.model[FK.placeholder,""] != ""
        {
            valueT.placeholderAttributedText = NSAttributedString(string: mo.model[FK.placeholder,""], attributes: [NSAttributedString.Key.font: FieldView.holderStyle.font, NSAttributedString.Key.foregroundColor: FieldView.holderStyle.color])
        }
        
        let value = mo.model[FK.value,""]
        if value == ""
        {
            let defaultV = mo.model[FK.defaultV,""]
            if defaultV != ""
            {
                mo.model.setObject(defaultV, forKey: FK.value as NSCopying)
                self.mo_model_reload()
            }
        }
        
        if mo.model["_editAble","YES"] == "NO"
        {
            valueT.isUserInteractionEnabled = false
        }
        else
        {
            valueT.isUserInteractionEnabled = true
        }
        
    }
    
    open func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        
        mo.model.setObject(textView.text!, forKey: FK.value as NSCopying)

        if text == "\n"
        {
            textView.resignFirstResponder()
            return false
        }
        return true
        
    }
    
    open func textViewDidEndEditing(_ textView: UITextView) {
        mo.model.setObject(textView.text!, forKey: "value" as NSCopying)
    }
    
    deinit
    {
        NotificationCenter.default.removeObserver(valueT, name: NSNotification.Name(rawValue: FK.SaveJoFormNotify), object: nil)
    }

    
    
}
